package com.lottery.ruok.lottrey.fragment;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.lottery.ruok.lottrey.R;
import com.lottery.ruok.lottrey.activity.PrizeListActivity;
import com.lottery.ruok.lottrey.adapter.TwoSidedStatisticsAdapter;
import com.lottery.ruok.lottrey.eventbusObject.Tabs;
import com.lottery.ruok.lottrey.retrofitModule.RetrofitBase;
import com.lottery.ruok.lottrey.retrofitModule.RxHelper;
import com.lottery.ruok.lottrey.retrofitModule.RxSubscribe;
import com.lottery.ruok.lottrey.retrofitModule.request.LotteryStatTwoRequest;
import com.lottery.ruok.lottrey.retrofitModule.response.LotteryStatOneResponse;
import com.lottery.ruok.lottrey.retrofitModule.response.LotteryStatTwoResponse;
import com.lottery.ruok.lottrey.ui.MyListView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by ASUS on 2017/7/27.
 */

public class TwoSidedStatisticsFragment extends BaseFragment {
    @BindView(R.id.listView)
    MyListView listView;

    private LotteryStatTwoResponse lotteryStatTwoResponses;
    private TwoSidedStatisticsAdapter twoSidedStatisticsAdapter;
    private LotteryStatOneResponse data;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        EventBus.getDefault().register(this);
    }

    @Override
    public void onDetach() {
        super.onDetach();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void setTabs(Tabs tabs){
        if(tabs.isCanListScroll()){
            listView.setCanListScroll(true);
        } else {
            listView.setCanListScroll(false);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.statistic_number_fragment, container, false);

        ButterKnife.bind(this, view);

        init();
        return view;
    }

    private void init() {
        getList();
    }

    public void getList(){
        if((getActivity()) != null) {
            RetrofitBase.getRetrofit().create(LotteryStatTwoRequest.class)
                    .getResult(((PrizeListActivity)getActivity()).currentCategory, ((PrizeListActivity)getActivity()).currentDate)
                    .compose(RxHelper.<LotteryStatTwoResponse>handleResult())
                    .subscribe(new RxSubscribe<LotteryStatTwoResponse>(getActivity()) {
                        @Override
                        protected void _onNext(LotteryStatTwoResponse lotteryStatTwoResponse) {
                            if(getActivity() != null && isAdded()){
                                lotteryStatTwoResponses = lotteryStatTwoResponse;

                                if (lotteryStatTwoResponses.getNumbers() != null && lotteryStatTwoResponses.getNumber_times() != null
                                        && !lotteryStatTwoResponses.getNumbers().equals("")
                                        && !lotteryStatTwoResponses.getNumber_times().equals("")) {
                                    ArrayList<String> list = new ArrayList();
                                    list.add("号码出现次数");
                                    list.add(lotteryStatTwoResponses.getNumbers());
                                    list.add(lotteryStatTwoResponses.getNumber_times());
                                    lotteryStatTwoResponses.getBall().add(0, list);
                                }

                                twoSidedStatisticsAdapter = new TwoSidedStatisticsAdapter(getActivity(), lotteryStatTwoResponses);
                                listView.setAdapter(twoSidedStatisticsAdapter);
                            }
                        }

                        @Override
                        protected boolean showDialog() {
                            return false;
                        }
                    });
        }
    }

    public void setData(LotteryStatOneResponse data) {
        this.data = data;
        getList();
    }
}
